package com.jianhua.appmarket.module.service.system.impl;

import com.jianhua.appmarket.common.util.SnowflakeKeyUtil;
import com.jianhua.appmarket.module.dao.system.DeptDao;
import com.jianhua.appmarket.module.dao.system.OrgDeptDao;
import com.jianhua.appmarket.module.entity.system.Dept;
import com.jianhua.appmarket.module.entity.system.OrgDept;
import com.jianhua.appmarket.module.entity.system.User;
import com.jianhua.appmarket.module.model.system.DeptParam;
import com.jianhua.appmarket.module.model.system.DeptVO;
import com.jianhua.appmarket.module.model.system.UserVO;
import com.jianhua.appmarket.module.service.system.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class DeptService implements IDeptService {

    @Autowired
    private DeptDao deptDao;

    @Autowired
    private OrgDeptDao orgDeptDao;

    @Override
    public List<DeptVO> listAllDeptByOrgId(Long orgId) {
        DeptParam param = new DeptParam();
        param.setOrgId(orgId);
        List<DeptVO> deptList = deptDao.listByParam(param);
        return buildDeptTree(deptList,0L);
    }

    @Override
    public List<DeptVO> listBranchByParentId(Long parentId) {
        DeptParam param = new DeptParam();
        param.setParentId(parentId);
        return deptDao.listByParam(param);
    }

    @Override
    @Transactional
    public Boolean deleteDept(Long deptId) {
        OrgDept orgDept = new OrgDept();
        orgDept.setDeptId(deptId);
        return deptDao.deleteById(deptId) > 0 && orgDeptDao.delete(orgDept) > 0 ;
    }

    @Transactional
    @Override
    public Boolean addOrEditDept(Dept dept, UserVO user) {
        if(Objects.isNull(dept.getId())){
            dept.setId(SnowflakeKeyUtil.generateId());
            dept.setCreateUser(user.getNickName());
            dept.setUpdateUser(user.getNickName());

            OrgDept orgDept = new OrgDept();
            orgDept.setId(SnowflakeKeyUtil.generateId());
            orgDept.setDeptId(dept.getId());
            orgDept.setOrgId(user.getOrgId());
            orgDept.setCreateUser(user.getNickName());
            orgDept.setUpdateUser(user.getNickName());
            return deptDao.insert(dept) > 0 && orgDeptDao.insert(orgDept) > 0;
        }else {
            return editDept(dept,user);
        }
    }

    @Override
    public Boolean editDept(Dept dept,User user) {
        dept.setUpdateUser(user.getNickName());
        return deptDao.update(dept) > 0;
    }

    private List<DeptVO> buildDeptTree(List<DeptVO> deptList, Long parentId) {
        List<DeptVO> treeList = new ArrayList<>();
        deptList.forEach(dept -> {
            if (Objects.equals(parentId, dept.getParentId())) {
                dept.setChildren(buildDeptTree(deptList, dept.getId()));
                treeList.add(dept);
            }
        });
        return treeList;
    }


    /*    @Override
    public List<DeptVO> listAllDeptByOrgId(Long orgId) {
        DeptParam param = new DeptParam();
        param.setOrgId(orgId);
        param.setParentId(0L);
        return listDeptByParentId(param);
    }


    private List<DeptVO> listDeptByParentId(DeptParam param){
        List<DeptVO> children = deptDao.listByParams(param);
        if(null != children && children.size() > 0){
            children.forEach(c -> {
                param.setParentId(c.getId());
                c.setChildren(listDeptByParentId(param));
            });
        }
        return children;
    }*/
}
